<template>
	<view class="page">
		<view>
			<text>当前wifi：</text>
			<input type="text" v-model="wifi_name" placeholder="">
		</view>
		<view>
			<text>密码</text>
			<input type="text" v-model="password" placeholder="输入wifi密码">
		</view>
		<button @click="send">发送</button>
	</view>
</template>

<script>  
	export default {
		data() {
			return {
				wifi_name:'',
				password:'',
				udpSocket:null,
				infoList: [],
			}
		},
		async onLoad() {
			let _this=this;
			// uni.getConnectedWifi({
			// 	partialInfo:true,
			// 	success(res) {
			// 		console.log(res)
			// 		_this.wifi_name=res.wifi.SSID;
			// 	}
			// }) 
			
			// this.udpSocket= wx.createUDPSocket();
			// console.log(this.udpSocket)
			// const port = this.udpSocket.bind(5050) 
			// console.log('port',port)
			// this.udpSocket.onListening((res) => {
			//   console.log('onListening',res);
			// })
			// this.udpSocket.onMessage((res) => {
			// 	console.log(res);
			// })
		},
		async onShow() {
			 
		},
		onReachBottom(){
			 
		},
		methods: {
			send1(){
				// 初始化TCP实例
				        this.tcp = wx.createTCPSocket()
				        // tcp实例向服务端发起连接
				        this.tcp.connect({ address: '192.168.10.1', port: 5050 })
				        this.infoList.push(`向192.168.10.1:5050建立链接`)
				
				        this.tcp.onClose(() => {
				          this.infoList.push('关闭')
				          this.tcp.close()
				        })
				        this.tcp.offClose(() => {
				          this.infoList.push('offClose')
				        })
				        this.tcp.onConnect(() => {
				          this.infoList.push('连接成功')
				        })
				        this.tcp.offConnect(() => {
				          this.infoList.push('offConnect')
				        })
				        this.tcp.onError(error => {
				          this.infoList.push('异常:' + error)
				        })
				        this.tcp.onMessage(data => {
				          // data是一个对象，服务端发送的数据在message属性里，且message微信这边得到的是一个ArrayBuffer格式的数据，所以需要转换一下
				          this.infoList.push('接收到数据: ' + transitionArrayBufferToString(data.message))
				        }) 
			},
			send(){
				console.log(123)
				let data={
					wifi_name:this.wifi_name,
					password:this.password
				}
				uni.request({
					url:'http://192.168.10.1:5050',
					data:data,
					method:'POST',
					dataType:'json',
					success(res) {
						console.log('http://192.168.10.1:5050',res)
					},
					fail(err) {
						console.log(err)
					}
					
				})
				
				return
				console.log('JSON.stringify(data)',JSON.stringify(data))
				this.udpSocket.send({
				  address: '192.168.10.1',
				  port: 5050, //你需要发送的端口号
				  message:JSON.stringify(data) //你需要发送的信息
				})
				this.udpSocket.onError((err) => {
				  console.log('onError',err)
				})
			} 
		},
		computed:{
			 
		},
		watch:{
			 
		},
		components: { 
		}
	}
</script>

<style lang="less" scoped>
	 
</style>
